﻿#include "loadingwidget.h"
#include <QMovie>
#include <QToolButton>
#include <QDebug>
#include <QLabel>
#include <QVBoxLayout>
#define DEFAULTGIF ":/res/images/loading_.GIF"

LoadingWidget::LoadingWidget(QWidget *parent ,QString filename) :
    QWidget(parent),m_movie(0),lablGif(0)
{
    this->setObjectName("LoadingWidget");
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
//    setWindowFlags(windowFlags() | Qt::Dialog);
//        loadingWidget->setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);
    setAutoFillBackground(true);

//    setAttribute(Qt::WA_TransparentForMouseEvents);
    setFixedSize(parent->size()+QSize(15,37));
//    move(parent->pos());
//    setWindowOpacity(0.85);
//    show();
    if(filename.isEmpty())
        filename = DEFAULTGIF;

    vboxMain = new QVBoxLayout;

    lablGif = new QLabel(this);
    m_movie = new QMovie(this);
    m_movie->setFileName(filename);
    lablGif->resize(size());
    lablGif->setMovie(m_movie);
//    lablGif->move(QPoint(width()/2-m_movie->scaledSize().width()/2+lablGif->x(),lablGif->y()));

    //Layout
    vboxMain->addWidget(lablGif);
    lablGif->setAlignment(Qt::AlignCenter);
    if(vboxMain)
        setLayout(vboxMain);

    setButton(LoadingWidgetButton::cancel);

//    setStyleSheet(QString("LoadingWidget#%1{background:rgb(0,0,0,100);}").arg(objectName()));
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(0,0,0,180));
    setPalette(palette);
}

void LoadingWidget::play()
{
    if(this->isHidden())
        show();
    if(m_movie->state() == QMovie::NotRunning)
        m_movie->start();
    connect(m_movie,&QMovie::stateChanged,[=](QMovie::MovieState state){
        if(state == QMovie::Running){
            ;
//            if(m_map.size()){
//                QMap<int,bool>::const_iterator itor;
//                for(itor = m_map.constBegin();itor!=m_map.constEnd();itor ++)
//                {
//                    if(!itor.value())
//                        m_map.remove(itor.key());
//                }
//            }else{
//                m_movie->stop();
//                this->close();
//            }
        }
    });
}

void LoadingWidget::stop()
{
    if(m_movie->state() == QMovie::Running){
        m_movie->stop();
        this->close();
    }
}

LoadingWidget::~LoadingWidget()
{
    if(m_movie)
    {
        delete m_movie;
        m_movie = NULL;
    }
    if(lablGif){
        delete lablGif;
        lablGif = NULL;
    }
}

void LoadingWidget::setButton(LoadingWidget::LoadingWidgetButton btnType)
{
    QHBoxLayout *hbox = new QHBoxLayout;
    switch(btnType){
        case LoadingWidgetButton::ok:{
            QToolButton *btn_ok = new QToolButton(this);
            btn_ok->setText(QStringLiteral("确定"));
            btn_ok->setMinimumSize(50,25);
            hbox->addWidget(btn_ok);
            break;
        }
        case LoadingWidgetButton::cancel:{
            QToolButton *btn_cancel = new QToolButton(this);
            btn_cancel->setText(QStringLiteral("取消"));
            hbox->addWidget(btn_cancel);
            btn_cancel->setMinimumSize(50,25);
            connect(btn_cancel,&QToolButton::clicked,[=]{
                this->close();
            });
            break;
        }
    }
    if(vboxMain){
        vboxMain->addLayout(hbox);
        vboxMain->addSpacerItem(new QSpacerItem(5,50));
    }
}

void LoadingWidget::setData(QMap<int, bool> map)
{
    m_map = map;
}

void LoadingWidget::insertData(int num, bool flag)
{
    m_map.insert(num,flag);
}

void LoadingWidget::mouseReleaseEvent(QMouseEvent *e)
{
    emit onClicked();
    QWidget::mouseReleaseEvent(e);
}
